﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using BestLifePOS_PresentationLayer.Reports;
using System.Data.SqlClient;
using BestLifePOS_Core;


namespace BestLifePOS_PresentationLayer
{
    public partial class frmReportViewer : Form
    {
        int GvRptNo;
        string GvVal = "";
        public frmReportViewer(int PRptNo, string PVal)
        {
            InitializeComponent();
            GvRptNo = PRptNo;
            GvVal = PVal;
        }

        private void frmReportViewer_Load(object sender, EventArgs e)
        {

            //ConfigureCrystalReports();
            ConnectionInfo coninfo = constring();
            if (GvRptNo == 1)
            {
                ParameterFields ParamFeilds = new ParameterFields();
                ParameterField paramFeild = new ParameterField();

                //paramFeild.HasCurrentValue = true;
                paramFeild.Name = "@PRID";

                ParameterDiscreteValue PDV = new ParameterDiscreteValue();

                PDV.Value = GvVal;

                paramFeild.CurrentValues.Add(PDV);

                ParamFeilds.Add(paramFeild);

                //crystalReportViewer1.RefreshReport();
                crystalReportViewer1.ParameterFieldInfo = ParamFeilds;

                RptPRDetailsNotApproved rptPRN = new RptPRDetailsNotApproved();
                ReportDocument crDoc = (ReportDocument)rptPRN;
                //crystalReportViewer1.ReportSource = rptPRN;
                crystalReportViewer1.ParameterFieldInfo = ParamFeilds;

                crystalReportViewer1.ReportSource = crDoc;
                ReportSourceSetup(crDoc, coninfo);
            }

            if (GvRptNo == 2)
            {
                ParameterFields ParamFeilds = new ParameterFields();
                ParameterField paramFeild = new ParameterField();

                //paramFeild.HasCurrentValue = true;
                paramFeild.Name = "@POID";

                ParameterDiscreteValue PDV = new ParameterDiscreteValue();

                PDV.Value = GvVal;

                paramFeild.CurrentValues.Add(PDV);

                ParamFeilds.Add(paramFeild);

                //crystalReportViewer1.RefreshReport();
                crystalReportViewer1.ParameterFieldInfo = ParamFeilds;

                RptPODetailsNotApproved rptPO = new RptPODetailsNotApproved();
                ReportDocument crDoc = (ReportDocument)rptPO;
                //crystalReportViewer1.ReportSource = rptPRN;
                crystalReportViewer1.ParameterFieldInfo = ParamFeilds;

                crystalReportViewer1.ReportSource = crDoc;
                ReportSourceSetup(crDoc, coninfo);
            }

            if (GvRptNo == 3)
            {
                ParameterFields ParamFeilds = new ParameterFields();
                ParameterField paramFeild = new ParameterField();

                //paramFeild.HasCurrentValue = true;
                paramFeild.Name = "@GRNID";

                ParameterDiscreteValue PDV = new ParameterDiscreteValue();

                PDV.Value = GvVal;

                paramFeild.CurrentValues.Add(PDV);

                ParamFeilds.Add(paramFeild);

                //crystalReportViewer1.RefreshReport();
                crystalReportViewer1.ParameterFieldInfo = ParamFeilds;

                RptGRNDetailsNotApproved rptGRN = new RptGRNDetailsNotApproved();
                ReportDocument crDoc = (ReportDocument)rptGRN;
                //crystalReportViewer1.ReportSource = rptPRN;
                crystalReportViewer1.ParameterFieldInfo = ParamFeilds;

                crystalReportViewer1.ReportSource = crDoc;
                ReportSourceSetup(crDoc, coninfo);
            }

            if (GvRptNo == 4)
            {
                ParameterFields ParamFeilds = new ParameterFields();
                ParameterField paramFeild = new ParameterField();

                //paramFeild.HasCurrentValue = true;
                paramFeild.Name = "@RID";

                ParameterDiscreteValue PDV = new ParameterDiscreteValue();

                PDV.Value = GvVal;

                paramFeild.CurrentValues.Add(PDV);

                ParamFeilds.Add(paramFeild);

                //crystalReportViewer1.RefreshReport();
                crystalReportViewer1.ParameterFieldInfo = ParamFeilds;

                RptReciptPrint rptRpt= new RptReciptPrint();
                ReportDocument crDoc = (ReportDocument)rptRpt;
                //crystalReportViewer1.ReportSource = rptPRN;
                crystalReportViewer1.ParameterFieldInfo = ParamFeilds;

                crystalReportViewer1.ReportSource = crDoc;
                ReportSourceSetup(crDoc, coninfo);
            }

        }

        public void ReportSourceSetup(ReportDocument crDoc, ConnectionInfo crConnectionInfo)
        {
            // Each table in report needs to have logoninfo setup:
            Tables crTables = crDoc.Database.Tables;
            foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables)
            {
                TableLogOnInfo crTableLogonInfo = crTable.LogOnInfo;
                crTableLogonInfo.ConnectionInfo = crConnectionInfo;
                crTable.ApplyLogOnInfo(crTableLogonInfo);
                crTable.Location = crConnectionInfo.DatabaseName + ".dbo." +
                crTable.Location.Substring(crTable.Location.LastIndexOf(".") + 1);
                //crTable.Location  = WHSP_tock;1" 
            }
        }

        private ConnectionInfo constring()
        {
            ConnectionInfo crConnectionInfo = new ConnectionInfo();

            SqlConnection LvConn = new SqlConnection(RuntimeConstants.GvConnString);
            string LvConstring = "";
            LvConstring = LvConn.ConnectionString;
            string Lvserver = "";
            string LvDatabase = "";
            string LvUser = "";
            string LvPassword = "";
            //Data Source=BESTLIFEIT\\BESTLIFEIT;Initial Catalog=Bestlife_RMS;Persist Security Info=True;User ID=sa;Password=Pass@123
            Lvserver = LvConstring.Substring(12,21);
            LvDatabase = LvConstring.Substring(50,12);
            LvUser = LvConstring.Substring(98, 2);
            LvPassword = LvConstring.Substring(110, 8);
            crConnectionInfo.ServerName = Lvserver;
            crConnectionInfo.DatabaseName = LvDatabase;
            crConnectionInfo.UserID = LvUser;
            crConnectionInfo.Password = LvPassword;
            return crConnectionInfo;

        }


    }
}
